package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.monitor.entity.ErpCustomer;
import com.monitor.entity.ErpCustomerCar;
import com.monitor.service.ErpBidataService;
import com.monitor.service.ErpCustomerCarService;
import com.monitor.service.ErpCustomerService;
import com.monitor.service.ErpUserService;
import com.monitor.util.BlankUtil;
import com.monitor.util.Result;
import com.monitor.util.enums.PositonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户车辆表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-05
 */
@RestController
@RequestMapping("/erpCustomerCar")
@Api(description="客户车辆表")
public class ErpCustomerCarController {

    @Resource
    private ErpCustomerCarService customerCarService;
    @Resource
    private ErpUserService userService;
    @Resource
    private ErpCustomerService customerService;
    
    @Autowired
    private ErpBidataService erpBidataService;

    @RequestMapping("/inertOrUpdateCustomerCar")
    @ApiOperation("添加Or修改客户车辆信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<ErpCustomerCar> inertOrUpdateCustomerCar(ErpCustomerCar customerCar,int userId) {
    	 if (customerCar.getId()==null||customerCar.getId()==0){
	        ErpCustomer cust=customerService.selectById(customerCar.getCustomerId());
	        if(cust!=null) {
	        	List<ErpCustomerCar> car=customerCarService.selectList(new EntityWrapper<ErpCustomerCar>().eq("car_number", customerCar.getCarNumber()).eq("customer_id", customerCar.getCustomerId()));
	        	if(car!=null&&car.size()>0) {
	        		return new Result<>(1, "自编车号不可重复");
	        	}
	        }
	        if(customerCar.getCarShiBieNumber()!=null&&!customerCar.getCarShiBieNumber().equals("")) {
	        	List<ErpCustomerCar> car1=customerCarService.selectList(new EntityWrapper<ErpCustomerCar>().eq("car_shi_bie_number", customerCar.getCarShiBieNumber()));
	        	if(car1!=null&&car1.size()>0) {
	        		return new Result<>(1, "车辆识别编号不可重复");
	        	}
	        }
	        if(customerCar.getBianSuNumber()!=null&&!customerCar.getBianSuNumber().equals("")) {
	        	List<ErpCustomerCar> car1=customerCarService.selectList(new EntityWrapper<ErpCustomerCar>().eq("bian_su_number", customerCar.getBianSuNumber()));
	        	if(car1!=null&&car1.size()>0) {
	        		return new Result<>(1, "变速箱号不可重复");
	        	}
	        }
	        if(customerCar.getCarEngine()!=null&&!customerCar.getCarEngine().equals("")) {
	        	List<ErpCustomerCar> car1=customerCarService.selectList(new EntityWrapper<ErpCustomerCar>().eq("car_engine", customerCar.getCarEngine()));
	        	if(car1!=null&&car1.size()>0) {
	        		return new Result<>(1, "发动机号不可重复");
	        	}
	        }
	        if(customerCar.getXingShiNumber()!=null&&!customerCar.getXingShiNumber().equals("")) {
	        	List<ErpCustomerCar> car1=customerCarService.selectList(new EntityWrapper<ErpCustomerCar>().eq("xing_shi_number", customerCar.getXingShiNumber()));
	        	if(car1!=null&&car1.size()>0) {
	        		return new Result<>(1, "行驶证号不可重复");
	        	}
	        }
    	 }
        return customerCarService.addAndUpdate(customerCar, userId);
    }
   @RequestMapping("/updateCustomerCar")
    @ApiOperation("修改客户车辆信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<ErpCustomerCar> updateCustomerCar(ErpCustomerCar customerCar) {
        Result<ErpCustomerCar > result =new Result<>();
        int a=0;


       try {
           if (customerCar.getId()==null||customerCar.getId()==0){
               List<ErpCustomerCar> erpCustomerCars = customerCarService.selectList(new EntityWrapper<ErpCustomerCar>());
               for (int i = 0; i < erpCustomerCars.size(); i++) {
                   ErpCustomerCar erpCustomerCar =  erpCustomerCars.get(i);
                   if (erpCustomerCar.getCarPlate().equals(customerCar.getCarPlate())){
                       a=1;
                       result.setMsg("车牌号重复");
                       break;
                       
                   }
                   	
                   
               }
               
               if (a==0){
            	   List<ErpCustomerCar> car=customerCarService.selectList(new EntityWrapper<ErpCustomerCar>().eq("car_number", customerCar.getCarNumber()).eq("customer_id", customerCar.getCustomerId()).ne("id", customerCar.getId()));
                 	if(car!=null&&car.size()>0) {
                 		return new Result<>(1, "自编车号不可重复");
                 	}
                 }
                 if(customerCar.getCarShiBieNumber()!=null&&!customerCar.getCarShiBieNumber().equals("")) {
                 	List<ErpCustomerCar> car1=customerCarService.selectList(new EntityWrapper<ErpCustomerCar>().eq("car_shi_bie_number", customerCar.getCarShiBieNumber()).ne("id", customerCar.getId()));
                 	if(car1!=null&&car1.size()>0) {
                 		return new Result<>(1, "车辆识别编号不可重复");
                 	}
                 }
                 if(customerCar.getBianSuNumber()!=null&&!customerCar.getBianSuNumber().equals("")) {
                 	List<ErpCustomerCar> car1=customerCarService.selectList(new EntityWrapper<ErpCustomerCar>().eq("bian_su_number", customerCar.getBianSuNumber()).ne("id", customerCar.getId()));
                 	if(car1!=null&&car1.size()>0) {
                 		return new Result<>(1, "变速箱号不可重复");
                 	}
                 }
                 if(customerCar.getCarEngine()!=null&&!customerCar.getCarEngine().equals("")) {
                 	List<ErpCustomerCar> car1=customerCarService.selectList(new EntityWrapper<ErpCustomerCar>().eq("car_engine", customerCar.getCarEngine()).ne("id", customerCar.getId()));
                 	if(car1!=null&&car1.size()>0) {
                 		return new Result<>(1, "发动机号不可重复");
                 	}
                 }
                 if(customerCar.getXingShiNumber()!=null&&!customerCar.getXingShiNumber().equals("")) {
                 	List<ErpCustomerCar> car1=customerCarService.selectList(new EntityWrapper<ErpCustomerCar>().eq("xing_shi_number", customerCar.getXingShiNumber()).ne("id", customerCar.getId()));
                 	if(car1!=null&&car1.size()>0) {
                 		return new Result<>(1, "行驶证号不可重复");
                 	}
                   customerCarService.insert(customerCar);
                   result.setMsg("新增成功");
               }

           }else{
        	   
               customerCarService.insertOrUpdate(customerCar);
               result.setMsg("修改成功");
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
       return result;
    }

    @RequestMapping("/getcustomerCarByCustId")
    @ApiOperation("根据客户id客户车辆信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpCustomerCar> getcustomerCarByCustId(Integer custId) {
        Result<ErpCustomerCar> result=new Result<ErpCustomerCar>();
        try{
            ErpCustomerCar customerCar=customerCarService.selectOne(new EntityWrapper<ErpCustomerCar>().eq("customer_id",custId));
            result.setCount(1);
            result.setData(customerCar);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/removeCustomerCar")
    @ApiOperation("删除客户车辆信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "customerCarId", value = "客户车辆ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removeCustomerCar(Integer customerCarId) {
        Result<Integer> result=new Result<Integer>();
        try{
           
            ErpCustomerCar customerCar=customerCarService.selectById(customerCarId);
            ErpCustomer cust=customerService.selectById(customerCar.getCustomerId());
        	if(customerCar.getCarType1()!=null&&customerCar.getCarType1().equals("货运车辆")) {
        		int num=cust.getCusTruck()-1;
        		cust.setCusTruck(num);
        	}
        	if(customerCar.getCarType1()!=null&&customerCar.getCarType1().equals("客运车辆")) {
        		int num=cust.getCusTruck()-1;
        		cust.setCusTruck(num);
        	}
        	if(customerCar.getCarType2()!=null&&customerCar.getCarType2().equals("挂车")) {
        		int num=cust.getCusGCar()-1;
        		cust.setCusGCar(num);
        	}
        	if(customerCar.getCarType2()!=null&&customerCar.getCarType2().equals("拖头")) {
        		int num=cust.getCusTCar()-1;
        		cust.setCusTCar(num);
        	}
        	if(customerCar.getCarType2()!=null&&customerCar.getCarType2().equals("单机")) {
        		int num=cust.getCusDCar()-1;
        		cust.setCusDCar(num);
        	}
        	if(customerCar.getCarType2()!=null&&customerCar.getCarType2().equals("小型车")) {
        		int num=cust.getCusXCar()-1;
        		cust.setCusXCar(num);
        	}
        	int total=cust.getCusVehicletotal()-1;
        	cust.setCusVehicletotal(total);
        	customerService.updateAllColumnById(cust);
        	 result.setCount(1);
             result.setData(customerCarService.deleteById(customerCarId)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }
    @RequestMapping("/getCustomerCarBycustId")
    @ApiOperation("根据客户ID查询客户车辆信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpCustomerCar>> getCustomerCarBycustId(Integer custId) {
        Result<List<ErpCustomerCar>> result=new Result<List<ErpCustomerCar>>();
        try{
            List<ErpCustomerCar> customerCars=customerCarService.selectList(new EntityWrapper<ErpCustomerCar>().eq("customer_id",custId));
            //Page<ErpCustomerCar> page=new Page<ErpCustomerCar>(1,2);
            result.setCount(customerCars.size());
            result.setData(customerCars);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/getCustomerCarBycustIdPage")
    @ApiOperation("根据客户ID查询客户车辆信息分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Page<ErpCustomerCar>> getCustomerCarBycustIdPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                            @RequestParam(value="limit",defaultValue="10") int limit,Integer custId) {
        Result<Page<ErpCustomerCar>> result=new Result<Page<ErpCustomerCar>>();
        try{
            Page<ErpCustomerCar> page1=new Page<ErpCustomerCar>(page,limit);
            Page<ErpCustomerCar> carPage=customerCarService.selectPage(page1,new EntityWrapper<ErpCustomerCar>().eq("customer_id",custId));
            //Page<ErpCustomerCar> page=new Page<ErpCustomerCar>(1,2);
            result.setCount(carPage.getRecords().size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getCatInsurance")
    @ApiOperation("统计管理-车辆管理")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<Map<String,Object>>> getCatInsurance(@RequestParam(value = "page", defaultValue = "1") int page,
                                      @RequestParam(value="limit",defaultValue="10") int limit, Integer custId) {
        Result<List<Map<String,Object>>> result=new Result<List<Map<String,Object>>>();
        try{
            ErpCustomer customer=customerService.selectById(custId);
            List<ErpCustomer> customers=customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_sup_company",customer.getCusCode()).or().eq("cus_service_com",customer.getCusCode()));

            customers.add(customer);
            List<Integer> custIds=customers.stream().map(ErpCustomer::getId).collect(Collectors.toList());
            String custIdsStr= BlankUtil.listToString(custIds);
            List<Map<String,Object>> mapList=customerCarService.getCatInsurance(custIdsStr,page,limit);
            for (int i = 0; i < mapList.size(); i++) {
                Map<String, Object> map =  mapList.get(i);
                String customer_id = map.get("customer_id").toString();
                int i1 = Integer.parseInt(customer_id);
                ErpCustomer erpCustomer = customerService.selectById(i1);
                if (null!=erpCustomer){
                    map.put("custName",erpCustomer.getCusName());
                }

            }
            result.setCount(customerCarService.getCatInsuranceList(custIdsStr).size());
            result.setData(mapList);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getCarBycustId")
    @ApiOperation("统计管理-车辆统计")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<Map<String,Object>>> getTireByCarId(@RequestParam(value = "page", defaultValue = "1") int page,
                                                    @RequestParam(value="limit",defaultValue="10") int limit, Integer custId) {
        Result<List<Map<String,Object>>> result=new Result<List<Map<String,Object>>>();
        try{
            ErpCustomer customer=customerService.selectById(custId);
            List<ErpCustomer> customers=customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_sup_company",customer.getCusCode()).or().eq("cus_service_com",customer.getCusCode()));
            customers.add(customer);
            List<Integer> custIds=customers.stream().map(ErpCustomer::getId).collect(Collectors.toList());
            String custIdsStr= BlankUtil.listToString(custIds);
            List<Map<String,Object>> mapList=customerCarService.getCatInsurance(custIdsStr,page,limit);
            result.setCount(customerCarService.getCatInsuranceList(custIdsStr).size());
            result.setData(mapList);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }
//    @RequestMapping("/getCarBycustIdShai")
//    @ApiOperation("统计管理-车辆统计-详细筛选")
//    @ApiImplicitParams({
//           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query"),*/
//            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
//    })
//    public @ResponseBody
//    Result<List<Map<String,Object>>> getTireByCarId(@RequestParam(value = "page", defaultValue = "1") int page,
//                                                    @RequestParam(value="limit",defaultValue="10") int limit, Integer custId) {
//        Result<List<Map<String,Object>>> result=new Result<List<Map<String,Object>>>();
//        try{
//            ErpCustomer customer=customerService.selectById(custId);
//            List<ErpCustomer> customers=customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_sup_company",customer.getCusCode()).or().eq("cus_service_com",customer.getCusCode()));
//            customers.add(customer);
//            List<Integer> custIds=customers.stream().map(ErpCustomer::getId).collect(Collectors.toList());
//            String custIdsStr= BlankUtil.listToString(custIds);
//            List<Map<String,Object>> mapList=customerCarService.getCatInsurance(custIdsStr,page,limit);
//            result.setCount(customerCarService.getCatInsuranceList(custIdsStr).size());
//            result.setData(mapList);
//        }catch (Exception e){e.printStackTrace();}
//        return result;
//    }

    @RequestMapping("/selectPotionByCarId")
    @ApiOperation("根据车辆id查询胎位信息")
    @ApiImplicitParam(name = "carId", value = "车辆id", required = false, dataType = "int", paramType = "query")
    public Result<String[]> selectPotionByCarId(int carId){
    	ErpCustomerCar car=customerCarService.selectById(carId);
    	String[] str=PositonUtil.getChineseByTypeId(car.getCarType3());
    	return new Result<String[]>(str,str.length);
    }
    
    @RequestMapping("/selectPotionByCarPlate")
    @ApiOperation("根据车辆牌号查询胎位信息")
    @ApiImplicitParam(name = "carPlate", value = "车辆牌号", required = false, dataType = "int", paramType = "query")
    public Result<Map<String, Object>> selectPotionByCarPlate(String carPlate){
    	ErpCustomerCar car=customerCarService.selectByCarPlate(carPlate);
    	Map<String, Object> map=new HashMap<>();
    	int count=0;
    	if(car!=null) {
    		String[] str=PositonUtil.getChineseByTypeId(car.getCarType3());
    		map.put("carId", car.getId());
    		map.put("taiwei", str);
    	}
    	return new Result<Map<String, Object>>(map,count);
    }
    
    @RequestMapping("/selectByCarNumber")
    @ApiOperation("根据车辆牌号查询车辆id")
    @ApiImplicitParam(name = "carPlate", value = "车辆牌号", required = false, dataType = "int", paramType = "query")
    public Result<ErpCustomerCar> selectByCarNumber(String carPlate) {
    	try {
			ErpCustomerCar car=customerCarService.selectByCarPlate(carPlate);
			return new Result<>(car,1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<>(1,"系统异常");
		}
    }
}

